#!/usr/bin/python

import ApiDefinition
import ByteArraySerializer

class IpMoteDefinition(ApiDefinition.ApiDefinition):
    '''
    \ingroup ApiDefinition
    \brief  API definition for the IP mote.
   
    \note   This class inherits from ApiDefinition. It redefines the attributes of
            its parents class, but inherits the methods.
    '''
    
    STRING    = ApiDefinition.FieldFormats.STRING
    BOOL      = ApiDefinition.FieldFormats.BOOL
    INT       = ApiDefinition.FieldFormats.INT
    INTS      = ApiDefinition.FieldFormats.INTS
    HEXDATA   = ApiDefinition.FieldFormats.HEXDATA
    RC        = ApiDefinition.ApiDefinition.RC
    SUBID1    = ApiDefinition.ApiDefinition.SUBID1
    SUBID2    = ApiDefinition.ApiDefinition.SUBID2
    RC_OK     = ApiDefinition.ApiDefinition.RC_OK
    
    def __init__(self):
        self.serializer = ByteArraySerializer.ByteArraySerializer(self)
    
    def default_serializer(self,commandArray,fieldsToFill):
        '''
        \brief IpMgrDefinition-specific implementation of default serializer
        '''
        return self.serializer.serialize(commandArray,fieldsToFill)
        
    def deserialize(self,type,cmdId,byteArray):
        '''
        \brief IpMgrDefinition-specific implementation of deserializer
        '''
        return self.serializer.deserialize(type,cmdId,byteArray)
    
    # We redefine this attribute inherited from ApiDefinition. See
    # ApiDefinition for a full description of the structure of this field.
    fieldOptions = {
        RC: [
            [0x00, 'OK',                    ''],
            [0x01, 'ERROR',                 'generic error'],
            [0x02, 'RSVD1',                 'reserved'],
            [0x03, 'BUSY',                  ''],
            [0x04, 'INVALID_LEN',           ''],
            [0x05, 'INV_STATE',             ''],
            [0x06, 'UNSUPPORTED',           ''],
            [0x07, 'UNKNOWN_PARAM',         ''],
            [0x08, 'UNKNOWN_CMD',           ''],
            [0x09, 'WRITE_FAIL',            ''],
            [0x0A, 'READ_FAIL',             ''],
            [0x0B, 'LOW_VOLTAGE',           ''],
            [0x0C, 'NO_RESOURCES',          ''],
            [0x0D, 'INCOMPLETE_JOIN_INFO',  ''],
            [0x0E, 'NOT_FOUND',             ''],
            [0x0F, 'INVALID_VALUE',         ''],
            [0x10, 'ACCESS_DENIED',         ''],
        ],
        'autoJoin': [
            [0,    'no',                    ''],
            [1,    'yes',                   ''],
        ],
        'OTAPlockoutMode': [
            [0,    'not_allowed',           ''],
            [1,    'allowed',               ''],
        ],
        'serviceState': [
            [0,    'completed',             ''],
            [1,    'pending',               ''],
        ],
        'channel': [
            [0,    '2.405GHz',              ''],
            [1,    '2.410GHz',              ''],
            [2,    '2.415GHz',              ''],
            [3,    '2.420GHz',              ''],
            [4,    '2.425GHz',              ''],
            [5,    '2.430GHz',              ''],
            [6,    '2.435GHz',              ''],
            [7,    '2.440GHz',              ''],
            [8,    '2.445GHz',              ''],
            [9,    '2.450GHz',              ''],
            [10,   '2.455GHz',              ''],
            [11,   '2.460GHz',              ''],
            [12,   '2.465GHz',              ''],
            [13,   '2.470GHz',              ''],
            [14,   '2.475GHz',              ''],
            [15,   '2.480GHz',              ''],
        ],
        'ccaMode': [
            [0,    'off',                   ''],
            [1,    'on',                    ''],
        ],
        'protocol': [
            [0,    'udp',                   ''],
        ],
        'routingMode': [
            [0x00, 'enabled',               ''],
            [0x01, 'disabled',              ''],
        ],
        'mobilityType': [
            [0x00, 'UNUSED',                ''],
            [0x01, 'KNOWN',                 ''],
            [0x02, 'UNKNOWN',               ''],
            [0x03, 'MOBILE',                ''],
        ],
        'moteState': [
            [0,    'INIT',                  'Initializing'],
            [1,    'IDLE',                  'Idle, ready to be configured or join'],
            [2,    'SEARCHING',             'Received join command, searching for network'],
            [3,    'NEGO',                  'Sent initial join request'],
            [4,    'CONNECTED',             'Received at least one packet from the controller'],
            [5,    'OPERATIONAL',           'Configured by controller and ready to send data'],
            [6,    'DISCONNECTED',          'Disconnected from the network'],
        ],
        'moteEvents': [
            [0x01, 'BOOT',                  'The mote booted up'],
            [0x02, 'ALARMS_CHG',            'Value of alarms field changed'],
            [0x04, 'TIME_CHG',              'UTC time on the mote changed'],
            [0x08, 'JOINFAIL',              'Join operation failed'],
            [0x10, 'DISCON',                'The mote disconnected from the network'],
            [0x20, 'OPERATIONAL',           'Mote has connection to gateway to send data'],
            [0x40, 'RESERVED',              'Reserved'],
            [0x80, 'SVC_CHG',               'Mote service changed'],
            [0x100,'JOINSTART',             'Mote sent the 1st packet to initiate join sequence'],
        ],
        'moteAlarms': [
            [0x0,  'NONE',                  'No mote alarms detected'],
            [0x1,  'NVERR',                 'Detected an error in flash storage'],
            [0x2,  'LOWVOLTAGE',            'Detected low voltage'],
            [0x4,  'OTPERR',                'Detected an error in manufacturing parameters'],
            [0x8,  'SEND_NOTREADY',         'Detected an error in manufacturing parameters'],
        ],
        'serviceType': [
            [0,    'bandwidth',             'bandwidth'],
        ],
        'priority': [
            [0,    'LOW',                   'low'],
            [1,    'MED',                   'medium'],
            [2,    'HIGH',                  'high'],
            [3,    'CTRL',                  'control'],
        ],
        'txStatus': [
            [0,    'OK',                    'packet sent out ok'],
            [1,    'FAIL',                  'packet was dropped due to timeout or lack of route'],
        ],
        'txPower': [
            [0,    '0dBm',                  ''],
            [8,    '+8dBm',                 ''],
        ],
        'radiotest_type_t' : [
            [0,    'packet',                ''],
            [1,    'CM',                    ''],
            [2,    'CW',                    ''],
        ],
    }
    
    subCommandsSet = [
        {
            'id'         : 0x01,
            'name'       : 'macAddress',
            'description': 'Set the MAC address of the mote.',
            'request'    : [
                ['macAddress',              HEXDATA,  8,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x02,
            'name'       : 'joinKey',
            'description': 'Set the key used by the mote when joining a network.',
            'request'    : [
                ['joinKey',                 HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x03,
            'name'       : 'networkId',
            'description': 'Set the network ID (PAN ID) of the mote.',
            'request'    : [
                ['netId',                   INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x04,
            'name'       : 'txPower',
            'description': 'Set the radio output power as a signed value in dBm, excluding any antenna gain. This command may be issued at any time and takes effect on the next transmission.',
            'request'    : [
                ['txPower',                 INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x06,
            'name'       : 'joinDutyCycle',
            'description': 'Set the ratio of active listen time to sleep time during network search. This command may be issued multiple times during the joining process. This command has no effect after join completion.',
            'request'    : [
                ['joinDutyCycle',           INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x0B,
            'name'       : 'eventMask',
            'description': 'Set the type of events sent in mote\'s events notification message. The default value of eventMask at mote reset is all 1\'s, i.e. all events are enabled.\n\'1\' in the corresponding bit indicates that the mote should generate the corresponding event.\n\'0\' indicates that the mote should not generate the corresponding event.',
            'request'    : [
                ['eventMask',               INT,      4,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x15,
            'name'       : 'OTAPlockout',
            'description': '',
            'request'    : [
                ['OTAPlockoutMode',         INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x16,
            'name'       : 'macMicKey',
            'description': 'Set the key used for MAC authentication.',
            'request'    : [
                ['macMicKey',               HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x17,
            'name'       : 'shortAddress',
            'description': 'Set the mote\'s short address.',
            'request'    : [
                ['shortAddress',            INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x18,
            'name'       : 'ipv6Address',
            'description': 'Set the mote\'s IPv6 address.',
            'request'    : [
                ['ipv6Address',             HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x19,
            'name'       : 'ccaMode',
            'description': '',
            'request'    : [
                ['ccaMode',                 INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x1A,
            'name'       : 'Channels',
            'description': '',
            'request'    : [
                ['bitmap',                  INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x1B,
            'name'       : 'advGraph',
            'description': '',
            'request'    : [
                ['graphId',                 INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x1C,
            'name'       : 'hrTimer',
            'description': 'Set the duration (in seconds) between consecutive health reports.',
            'request'    : [
                ['hrTimer',                 INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x1D,
            'name'       : 'routingMode',
            'description': '',
            'request'    : [
                ['routingMode',             INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x1E,
            'name'       : 'appInfo',
            'description': '',
            'request'    : [
                ['vendorId',                INT,      2,   None],
                ['appId',                   INT,      1,   None],
                ['appMajorSwRev',           INT,      1,   None],
                ['appMinorSwRev',           INT,      1,   None],
                ['appPatchNum',             INT,      1,   None],
                ['appBuildNum',             INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x1F,
            'name'       : 'powerSrcInfo',
            'description': '',
            'request'    : [
                ['MaxStCurrent',            INT,      2,   None],
                ['MinLifetime',             INT,      1,   None],
                ['currentLimit_1',          INT,      2,   None],
                ['dischargePeriod_1',       INT,      2,   None],
                ['rechargePeriod_1',        INT,      2,   None],
                ['currentLimit_2',          INT,      2,   None],
                ['dischargePeriod_2',       INT,      2,   None],
                ['rechargePeriod_2',        INT,      2,   None],
                ['currentLimit_3',          INT,      2,   None],
                ['dischargePeriod_3',       INT,      2,   None],
                ['rechargePeriod_3',        INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x21,
            'name'       : 'mobilityType',
            'description': '',
            'request'    : [
                ['mobilityType',            INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x22,
            'name'       : 'advKey',
            'description': 'Set the key used by the mote for advertisement packets',
            'request'    : [
                ['advKey',                  HEXDATA,  16,  None],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
        {
            'id'         : 0x24,
            'name'       : 'autoJoin',
            'description': '',
            'request'    : [
                ['autoJoin',                INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                ],
            },
        },
    ]
    
    subCommandsGet = [
        {
            'id'         : 0x01,
            'name'       : 'macAddress',
            'description': 'Get the MAC address of the mote.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['macAddress',          HEXDATA,  8,   None],
                ],
            },
        },
        {
            'id'         : 0x03,
            'name'       : 'networkId',
            'description': 'Get the network ID (pan ID) of the mote.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['netId',               INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x04,
            'name'       : 'txPower',
            'description': 'Get the radio output power as a signed value in dBm, excluding any antenna gain.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['txPower',             INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x06,
            'name'       : 'joinDutyCycle',
            'description': 'Get the join duty cycle currently set.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['joinDutyCycle',       INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x0C,
            'name'       : 'moteInfo',
            'description': 'Get information about the mote\'s hardware and software.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['apiVersion',          INT,      1,   None],
                    ['serialNumber',        HEXDATA,  8,   None],
                    ['hwModel',             INT,      1,   None],
                    ['hwRev',               INT,      1,   None],
                    ['swMajorRev',          INT,      1,   None],
                    ['swMinorRev',          INT,      1,   None],
                    ['swPatchNum',          INT,      1,   None],
                    ['swBuildNum',          INT,      2,   None],
                    ['bootloaderVer',       INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x0D,
            'name'       : 'networkInfo',
            'description': 'Get network-related parameters.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['macAddr',             HEXDATA,  8,   None],
                    ['moteId',              INT,      2,   None],
                    ['netId',               INT,      2,   None],
                    ['slotSize',            INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x0E,
            'name'       : 'moteStatus',
            'description': 'Get information about the mote\'s status.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['moteState',           INT,      1,   True],
                    ['reserved',            INT,      1,   None],
                    ['reserved_1',          INT,      2,   None],
                    ['numParents',          INT,      1,   None],
                    ['moteAlarms',          INT,      4,   True],
                    ['reserved_2',          INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x0F,
            'name'       : 'time',
            'description': 'Get the current time of the mote.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['uptime',              INT,      4,   None],
                    ['utcSecs',             INT,      8,   None],
                    ['utcUsecs',            INT,      4,   None],
                    ['asn',                 HEXDATA,  5,   None],
                    ['asnOffset',           INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x10,
            'name'       : 'charge',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['qTotal',              INT,      4,   None],
                    ['moteUpTime',          INT,      4,   None],
                    ['tempInt',             INT,      1,   None],
                    ['tempFrac',            INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x11,
            'name'       : 'radioTestRxStats',
            'description': 'Get statistics collected during the receive tests.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['rxOk',                INT,      2,   None],
                    ['rxFailed',            INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x15,
            'name'       : 'OTAPlockout',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['OTAPlockoutMode',     INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x17,
            'name'       : 'shortAddress',
            'description': 'Get the short address of the mote.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['shortAddress',        INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x18,
            'name'       : 'ipv6Address',
            'description': 'Get the IPv6 address of the mote.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['ipv6Address',         HEXDATA,  16,  None],
                ],
            },
        },
        {
            'id'         : 0x1D,
            'name'       : 'routingMode',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['routingMode',         INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x1E,
            'name'       : 'appInfo',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['vendorId',            INT,      2,   None],
                    ['appId',               INT,      2,   None],
                    ['appMajorSwRev',       INT,      1,   None],
                    ['appMinorSwRev',       INT,      1,   None],
                    ['appPatchNum',         INT,      1,   None],
                    ['appBuildNum',         INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x1F,
            'name'       : 'powerSrcInfo',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['MaxStCurrent',        INT,      2,   None],
                    ['MinLifetime',         INT,      1,   None],
                    ['currentLimit_1',      INT,      2,   None],
                    ['dischargePeriod_1',   INT,      2,   None],
                    ['rechargePeriod_1',    INT,      2,   None],
                    ['currentLimit_2',      INT,      2,   None],
                    ['dischargePeriod_2',   INT,      2,   None],
                    ['rechargePeriod_2',    INT,      2,   None],
                    ['currentLimit_3',      INT,      2,   None],
                    ['dischargePeriod_3',   INT,      2,   None],
                    ['rechargePeriod_3',    INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x20,
            'name'       : 'powerCostInfo',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['maxTxCost',           INT,      1,   None],
                    ['maxRxCost',           INT,      1,   None],
                    ['minTxCost',           INT,      1,   None],
                    ['minRxCost',           INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x21,
            'name'       : 'mobilityType',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['mobilityType',        INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x22,
            'name'       : 'advKey',
            'description': 'Get the key used for advertisement packets.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['advKey',              HEXDATA,  16,  None],
                ],
            },
        },
        {
            'id'         : 0x23,
            'name'       : 'sizeInfo',
            'description': 'Get information about the mote\'s storage capacity.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['maxFrames',           INT,      1,   None],
                    ['maxLinks',            INT,      2,   None],
                    ['maxNeighbors',        INT,      1,   None],
                    ['maxRoutes',           INT,      1,   None],
                    ['maxGraphs',           INT,      1,   None],
                    ['maxMacQueue',         INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x24,
            'name'       : 'autoJoin',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    ['autoJoin',            INT,      1,   True],
                ],
            },
        },
    ]
    
    # We redefine this attribute inherited from ApiDefinition. See
    # ApiDefinition for a full description of the structure of this field.
    commands = [
        {
            'id'         : 0x01,
            'name'       : 'setParameter',
            'description': 'Set the value of a parameter.',
            'request'    : [
                [SUBID1,                    INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    [SUBID1,                INT,      1,   None],
                ]
            },
            'subCommands': subCommandsSet,
        },
        {
            'id'         : 0x02,
            'name'       : 'getParameter',
            'description': 'Get the value of a parameter.',
            'request'    : [
                [SUBID1,                    INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    [SUBID1,                INT,      1,   None],
                ]
            },
            'subCommands': subCommandsGet,
        },
        {
            'id'         : 0x06,
            'name'       : 'join',
            'description': 'Request that the mote starts searching for a network and attempts to join.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x07,
            'name'       : 'disconnect',
            'description': 'Request that the mote disconnects from the network. After the disconnect completes, the mote will generate a \'disconnect\' event, and proceed to reset. If the mote is not in the network, the \'disconnect\' event will be generated immediately. This command may be issued at any time.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x08,
            'name'       : 'reset',
            'description': 'Request the mote to reset. The device will reset after sending a response to this command.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x09,
            'name'       : 'lowPowerSleep',
            'description': 'Shut down all peripherals and put the mote into a low-power sleep mode. The actual command execution occurs after the response is sent. This command may be issued at any time and will cause the mote to interrupt all in-progress network operation. If graceful disconnect is desired, the \'disconnect\' command should be used prior to issuing this command.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x0C,
            'name'       : 'radioTestRx',
            'description': 'Clear all previously collected statistics and initiate radio receive on the specified channel for the specified duration of time. The mote keeps statistics of packets received with and without error during the test. Use getParam.radioTestRxStats to get those statistics.',
            'request'    : [
                ['mask',                    INT,      2,   None],
                ['time',                    INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,  1,   True]
                ],
            },
        },
        {
            'id'         : 0x10,
            'name'       : 'clearNv',
            'description': 'Put the mote\'s non-volatile storage into factory-default state.',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x11,
            'name'       : 'requestService',
            'description': 'Request a change of bandwidth to another device in the mesh. This command may only be used to update service to a device with an existing connection (session). A service is uniquely identified by (destination,type) tuple.\nWhenever a change in bandwidth assignment occurs, the application receives a Service Changed event that it can use as a trigger to read the new service allocation.',
            'request'    : [
                ['destAddr',                INT,      2,   None],
                ['serviceType',             INT,      1,   True],
                ['periodOrLatency',         INT,      4,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x12,
            'name'       : 'getServiceInfo',
            'description': '',
            'request'    : [
                ['destAddr',                INT,      2,   None],
                ['serviceType',             INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['destAddr',            INT,      2,   None],
                    ['serviceType',         INT,      1,   True],
                    ['serviceState',        INT,      1,   True],
                    ['periodOrLatency',     INT,      4,   None],
                ],
            },
        },
        {
            'id'         : 0x13,
            'name'       : 'writeFile',
            'description': 'Write byte payload at some offset in some file.',
            'request'    : [
                ['descriptor',              INT,      1,   None],
                ['offset',                  INT,      4,   None],
                ['len',                     INT,      4,   None],
                ['payload',                 HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x14,
            'name'       : 'readFile',
            'description': 'Read some bytes from a file at a given offset.',
            'request'    : [
                ['descriptor',              INT,      1,   None],
                ['offset',                  INT,      2,   None],
                ['len',                     INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['lengthRead',          INT,      1,   None],
                    ['payload',             HEXDATA,  None,None],
                ],
            },
        },
        {
            'id'         : 0x15,
            'name'       : 'openSocket',
            'description': 'Create a socket. Returns the ID of the socket, which can be used for subsequent.',
            'request'    : [
                ['protocol',                INT,      1,   True],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['socketId',            INT,      1,   None],
                ],
            },
        },
        {
            'id'         : 0x16,
            'name'       : 'closeSocket',
            'description': 'Close a particular socket.',
            'request'    : [
                ['socketId',                INT,      1,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x17,
            'name'       : 'bindSocket',
            'description': 'Bind the a socket previously opened to an address. When a socket is created, it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bindSocket command before the socket can accept connections from other hosts.',
            'request'    : [
                ['socketId',                INT,      1,   None],
                ['port',                    INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x18,
            'name'       : 'sendTo',
            'description': 'Send a packet into the network. If the command returns OK, the mote has accepted the packet and has queueed it up for transmission. A txDone notification will be issued when the packet has been sent, if and only if the packetId passed in this command is different from 0xffff. You can set the packet ID to any value. The notification will contain the packet ID of the packet just sent, allowing you to assiciate the notification to a particular packet. Once the packet is sent succesfully, it is removed from the queues.',
            'request'    : [
                ['socketId',                INT,      1,   None],
                ['destIP',                  HEXDATA,  16,  None],
                ['destPort',                INT,      2,   None],
                ['serviceType',             INT,      1,   True],
                ['priority',                INT,      1,   True],
                ['packetId',                INT,      2,   None],
                ['payload',                 HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x21,
            'name'       : 'tunnel',
            'description': '',
            'request'    : [
                ['payload',                 HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                    ['payload',             HEXDATA,  None,None],
                ],
            },
        },
        {
            'id'         : 0x23,
            'name'       : 'sendToMac',
            'description': '',
            'request'    : [
                ['priority',                INT,      2,   True],
                ['linkCtrl',                INT,      1,   None],
                ['txDoneNotif',             INT,      1,   None],
                ['reserved',                INT,      4,   None],
                ['packetId',                INT,      2,   None],
                ['frameId',                 INT,      1,   None],
                ['timeslot',                INT,      4,   None],
                ['channel',                 INT,      1,   True],
                ['payload',                 HEXDATA,  None,None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x24,
            'name'       : 'search',
            'description': '',
            'request'    : [
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x28,
            'name'       : 'radioTestTx',
            'description': '',
            'request'    : [
                ['type',                    INT,      1,   None],
                ['mask',                    INT,      2,   None],
                ['numRepeats',              INT,      2,   None],
                ['txPower',                 INTS,     1,   None],
                ['numPackets',              INT,      1,   None],
                ['pkSize1',                 INT,      1,   None],
                ['gap1',                    INT,      2,   None],
                ['pkSize2',                 INT,      1,   None],
                ['gap2',                    INT,      2,   None],
                ['pkSize3',                 INT,      1,   None],
                ['gap3',                    INT,      2,   None],
                ['pkSize4',                 INT,      1,   None],
                ['gap4',                    INT,      2,   None],
                ['pkSize5',                 INT,      1,   None],
                ['gap5',                    INT,      2,   None],
                ['pkSize6',                 INT,      1,   None],
                ['gap6',                    INT,      2,   None],
                ['pkSize7',                 INT,      1,   None],
                ['gap7',                    INT,      2,   None],
                ['pkSize8',                 INT,      1,   None],
                ['gap8',                    INT,      2,   None],
                ['pkSize9',                 INT,      1,   None],
                ['gap9',                    INT,      2,   None],
                ['pkSize10',                INT,      1,   None],
                ['gap10',                   INT,      2,   None],
            ],
            'response'   : {
                'FIELDS':  [
                    [RC,                    INT,      1,   True],
                ],
            },
        },
    ]
    
    # We redefine this attribute inherited from ApiDefinition. See
    # ApiDefinition for a full description of the structure of this field.
    notifications = [
        {
            'id'         : 0x0D,
            'name'       : 'timeNotif',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['uptime',              INT,      4,   None],
                    ['utcSecs',             INT,      8,   None],
                    ['utcUsecs',            INT,      4,   None],
                    ['asn',                 HEXDATA,  5,   None],
                    ['asnOffset',           INT,      2,   None],
                ],
            },
        },
        {
            'id'         : 0x0F,
            'name'       : 'eventsNotif',
            'description': 'Informs the application that some new events occurred since the last event notification. If the mote cannot send a packet (due to full queue or lack of buffers), it will retry once a minute until it succeeds.',
            'response'   : {
                'FIELDS':  [
                    ['moteEvents',          INT,      4,   True],
                    ['moteState',           INT,      1,   True],
                    ['moteAlarms',          INT,      4,   True],
                ],
            },
        },
        {
            'id'         : 0x19,
            'name'       : 'receiveNotif',
            'description': 'Informs the application that a UDP packet was received.',
            'response'   : {
                'FIELDS':  [
                    ['socketId',            INT,      1,   None],
                    ['srcIP',               HEXDATA,  16,  None],
                    ['srcPort',             INT,      2,   None],
                    ['payload',             HEXDATA,  None,None],
                ],
            },
        },
        # serviceAssignedNotif 
        {
            'id'         : 0x24,
            'name'       : 'macRxNotif',
            'description': '',
            'response'   : {
                'FIELDS':  [
                    ['payload',             HEXDATA,  None,None],
                ],
            },
        },
        {
            'id'         : 0x25,
            'name'       : 'txDoneNotif',
            'description': 'Informs the application that the mote has finished sending a packet. This notification will only be generated if the user has provided a valid packet ID when calling the sendTo command.',
            'response'   : {
                'FIELDS':  [
                    ['packetId',            INT,      2,   None],
                    ['txStatus',            INT,      1,   True],
                ],
            },
        },
        {
            'id'         : 0x26,
            'name'       : 'advReceived',
            'description': 'The notification is generated by the mote when it is in promiscuous listen state and it receives an advertisement',
            'response'   : {
                'FIELDS':  [
                    ['netId',             INT,      2,   None],
                    ['moteId',            INT,      2,   None],
                    ['rssi',              INTS,     1,   None],
                    ['joinPriority',      INT,      1,   None],
                ],
            },
        },
    ]
